<template>
  <div class="app-container">
    <template>
      <el-row>
        <el-col :span="24">
          <div class="h_top">
            <div class="search_box">
              <el-form :model="query" :inline="true">
                <el-row :gutter="10">
                  <el-col :xl="4" :lg="4" :md="5" :sm="5">
                    <el-form-item>
                      <div>编 号</div>
                      <el-input v-model="query.coding" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="4" :lg="4" :md="5" :sm="5">
                    <el-form-item>
                      <div>名 称</div>
                      <el-input v-model="query.name" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="4" :lg="4" :md="5" :sm="5">
                    <el-form-item>
                      <div>状 态</div>
                      <el-select v-model="query.status" size="mini">
                        <el-option v-for="(item, index) in theStatus" :key="index" :label="item" :value="index" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :xl="2" :lg="2" :md="2" :sm="2">
                    <el-form-item>
                      <div>操 作</div>
                      <el-button type="primary" icon="el-icon-search" size="mini" @click="search">查找</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
            <div class="toolBars">
              <div class="buttonBox">
                <el-dropdown :hide-on-click="false">
                  <el-button type="primary" icon="el-icon-tickets" size="mini" @click="exportHandler">导出<i class="el-icon-arrow-down el-icon--right" /></el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-checkbox-group v-model="needExport">
                      <el-dropdown-item><el-checkbox label="coding">编号</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="name">名称</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="cost.coding">成本代码</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="statusOut">状态</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="typeOut">类型</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="admin.name">维护负责人</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="admin.phone">电话</el-checkbox></el-dropdown-item>
                    </el-checkbox-group>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-document-add" size="mini" @click="addHandler">增加</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-delete" size="mini" :disabled="!selection.length" @click="remove">删除</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="reloadHandler">刷新</el-button>
              </div>
              <div class="fr">总数：<span>{{ total }}</span></div>
            </div>
          </div>
        </el-col>
      </el-row>
    </template>
    <div>
      <el-table v-loading="loading" :data="menuList" size="small" stripe border :header-cell-style="{background:'#eef1f6',color:'#606266'}" style="width: 100%" @selection-change="selectionHandler">
        <el-table-column type="index" label="序号" min-width="20%" align="center" />
        <el-table-column type="selection" min-width="20%" align="center" />
        <el-table-column prop="coding" label="编号" min-width="50%" align="center" />
        <el-table-column prop="name" label="名称" min-width="50%" align="center" />
        <el-table-column prop="status" label="状态" min-width="50%" align="center" :formatter="statusFormat" />
        <el-table-column fixed="right" label="操作" min-width="80%" align="center">
          <template scope="scope">
            <el-button type="primary" size="mini" @click="updateHandler(scope.row.id)">编辑</el-button>
            <el-button type="danger" size="mini" @click="confirmDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="page" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
    </div>
  </div>
</template>
<script>
import API from '@/api/system/menu'
import { typeCategory, theStatus } from '@/utils/options'
import xlsx from 'xlsx'
import exportMethod from '@/mixins/export'
export default {
  inject: ['reload', 'goUrl', 'delOne', 'delMany', 'getStatusFormat', 'getTypeFormat'],
  components: {
  },
  mixins: [exportMethod],
  data() {
    return {
      needExport: [],
      loading: false,
      total: 0,
      query: { page: 1, limit: 15 },
      menuList: [],
      selection: [],
      theStatus,
      typeCategory
    }
  },
  watch: {},
  created() {
    this.index()
  },
  methods: {
    index() {
      this.loading = true
      API.menuList(this.query).then(response => {
        this.total = response.data.total
        this.menuList = response.data.data
      }).finally(() => {
        this.loading = false
      })
    },
    search() {
      this.query.page = 1
      this.index()
    },
    selectionHandler(selection) {
      this.selection = selection
    },
    typeFormat(row) {
      return this.getTypeFormat(row)
    },
    exportHandler() {
      if (this.selection.length === 0) {
        this.$message.error('请选择要到导出的数据')
        return false
      }
      this.selection.forEach((item) => {
        if (item.status === 0) {
          item.statusOut = '测试'
        } else if (item.status === 1) {
          item.statusOut = '启用'
        } else if (item.status === 2) {
          item.statusOut = '临时移除'
        } else if (item.status === 3) {
          item.statusOut = '停用'
        }
        if (item.type === 0) {
          item.typeOut = '系统'
        } else if (item.type === 1) {
          item.typeOut = '自定义'
        }
      })
      const data = []
      const titleObj = {
        'coding': '编号',
        'name': '名称',
        'cost.coding': '成本代码',
        'statusOut': '状态',
        'typeOut': '类型',
        'admin.name': '维护负责人',
        'admin.phone': '电话'
      }
      if (this.needExport.length) {
        const title = []
        for (const key of this.needExport) {
          title.push(titleObj[key])
        }
        data.push(title)
      } else {
        data.push(Object.values(titleObj))
      }
      this.selection.forEach((item) => {
        if (this.needExport.length) {
          const d = []
          for (const key of this.needExport) {
            if (key.includes('.')) {
              let value = item
              const tag = key.split('.')
              for (const k of tag) {
                value = value[k]
              }
              d.push(value)
            } else {
              d.push(item[key])
            }
          }
          data.push(d)
        } else {
          data.push([
            item.coding,
            item.name,
            item.cost.coding,
            item.statusOut,
            item.typeOut,
            item.admin.name,
            item.admin.phone
          ])
        }
      })
      const workbook = {
        SheetNames: ['menu'],
        Sheets: {
          'menu': xlsx.utils.aoa_to_sheet(data)
        }
      }
      this.exportExec(workbook, '总菜单数据报表.xlsx')
    },
    remove() {
      const selection = this.selection
      this.delMany(API, selection)
    },
    confirmDel(id) {
      this.delOne(API, id, false)
    },
    addHandler() {
      this.goUrl('/system/menu/menu_add')
    },
    updateHandler(id) {
      const url = `/system/menu/menu_edit/${id}`
      this.goUrl(url)
    },
    reloadHandler() {
      this.reload()
    },
    statusFormat(row) {
      return this.getStatusFormat(row)
    }
  }
}
</script>
